@echo off

setlocal enabledelayedexpansion

call ..\setEnv.bat

set info_m=
set /a ret=0

set /a is_err_if_lines=2


echo is_err_if_lines = %is_err_if_lines%

del .\db\log.log

echo db upgrade starting ... ...

if not exist ..\version_list.txt (
  echo ..\version_list.txt does not exist, no version info, please check again.
  set info_m=with errors.
  set /a ret=-100
  goto finish
)

if exist version.txt del version.txt  

sqlplus %dest_dbuser%/%dest_dbpwd%@%dest_tnsname% @version.sql > version.txt

if not exist version.txt (
  echo version.txt does not exist, retrieve test DB version may failed, please check again.
  set info_m=with errors.
  set /a ret=-100
  goto finish
)

@FOR /F "skip=13" %%k in (version.txt) do (
  echo %%k
  set testDBVer=%%k
  goto outloop
)

:outloop
 
for /f "eol=# skip=1" %%i in (..\version_list.txt) do (
set  vers=%%i

if !vers!==!testDBVer! (
  echo the first version[!vers!] input is equal to test DB version[!testDBVer!], no upgrade need! go ahead!
  goto success
)

echo upgrade - %%i ... ...
echo dbscript ... ...

cd ..\download_file\db\%%i\dbscript
if exist *.log del *.log
set NLS_LANG=AMERICAN_AMERICA.UTF8
sqlplus %dest_dbuser%/%dest_dbpwd%@%dest_tnsname% @upgrade_script.sql

if not exist *.log (
  echo dbscript log does not exist, maybe dbscript does not execute, please check again.
  set info_m=with errors.
  set /a ret=-100
  goto finish
)


:chk_dbscriptError
find /i "ORA-" *.log > 1.err_counts
set /a vars=0
echo init vars ::: !vars!
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
echo accumulate vars ::: !vars!
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i "SP2-" *.log > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i "PLS-" *.log > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i "IMP-" *.log > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i "EBER-" *.log > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i "DBERROR:" *.log > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i """O/S Message:""" *.log > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)



echo dbscript completed ... ...

echo plsql ... ...

cd ..\..\..\db\%%i\plsql
if exist *.log del *.log

set NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
sqlplus %dest_dbuser%/%dest_dbpwd%@%dest_tnsname% @upgrade_plsql.sql

if not exist *.log (
  echo plsql log does not exist, maybe plsql does not execute, please check again.
  set info_m=with errors.
  set /a ret=-100
  goto finish
)


:chk_plsqlError
find /i "ORA-" *.log > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto plsqlError_ex) else (del /q 1.err_counts)

find /i "SP2-" *.log > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto plsqlError_ex) else (del /q 1.err_counts)

find /i "PLS-" *.log > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto plsqlError_ex) else (del /q 1.err_counts)

find /i "IMP-" *.log > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto plsqlError_ex) else (del /q 1.err_counts)

find /i "EBER-" *.log > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto plsqlError_ex) else (del /q 1.err_counts)

find /i "DBERROR:" *.log > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto plsqlError_ex) else (del /q 1.err_counts)

find /i """O/S Message:""" *.log > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto plsqlError_ex) else (del /q 1.err_counts)


echo plsql completed ... ...

echo upgrade - %%i completed ... ...
cd ..\..\..\
)

goto success




:dbscriptError_ex
mkdir .\errorlogs
xcopy .\*.log .\errorlogs\  /Y >nul
xcopy .\1.err_counts .\errorlogs\  /Y >nul
echo meet dbscript error when upgrade !vers! , please see log file in !vers!.
set info_m=with errors.
set /a ret=-100
goto finish

:plsqlError_ex
mkdir .\errorlogs
xcopy .\*.log .\errorlogs\  /Y >nul
xcopy .\1.err_counts .\errorlogs\  /Y >nul
echo meet plsql error when upgrade !vers! , please see log file in !vers!.
set info_m=with errors.
set /a ret=-100
goto finish

:success
echo db upgrade completed ... ...
set info_m=without errors.
set /a ret=100

:finish
echo finish !info_m!

exit !ret!

endlocal

